package com.cheer.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cheer.dto.ApplicantContactUpdateDTO;
import com.cheer.dto.ApplicantUpdateDTO;
import com.cheer.enums.RoleEnum;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.ApplicantService;
import com.cheer.service.StandingBookService;
import com.cheer.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/applicant")
@Api(tags = "ApplicantController", description = "集体信息controller")
public class ApplicantController {

    @Autowired
    private ApplicantService applicantService;

    @Autowired
    private StandingBookService standingBookService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    /**
     * 查看集体信息
     */
    @GetMapping("/info")
    @ApiOperation("查看集体信息")
    public Result<ApplicantVO> getApplicant() {
        ApplicantVO applicantVO = applicantService.getByAccountId(userInfoHolder.getAccountId());
        return Result.success(applicantVO);
    }

    /**
     * 查看注册信息
     */
    @GetMapping("/register")
    public Result<RegisterVO> getRegister( Long accountId ) {

        if(ObjectUtil.isNull(accountId)){
            accountId = userInfoHolder.getAccountId();
        }
        RegisterVO registerVO = applicantService.getRegisterByAccountId(accountId);
        return Result.success(registerVO);
    }

    /**
     * 修改集体信息
     */
    @PostMapping("/update")
    public Result updateApplicant(@RequestBody @Valid ApplicantUpdateDTO applicantUpdateDTO) {

        Long accountId = userInfoHolder.getAccountId();

        if( ObjectUtil.isNotNull(applicantUpdateDTO.getAccountId())
                && userInfoHolder.getAccount().getRole() != RoleEnum.APPLICANT.getCode()){
            accountId = applicantUpdateDTO.getAccountId();
        }

        applicantService.updateApplicant(accountId, applicantUpdateDTO);
        return Result.success();
    }

    /**
     * 修改集体信息
     */
    @GetMapping("/update/checkdoc")
    public Result updateCheckDoc(String fileUrl, Long applicantId) {


        applicantService.updateCheckDoc(fileUrl, applicantId);
        return Result.success();
    }

    /**
     * 分页查询集体
     */
    @GetMapping("/page")
    public Result<Page<ApplicantPageVO>> getPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                 @RequestParam(required = false) Long orgId,
                                                 @RequestParam(value = "orgName", required = false) String applicantName,
                                                 @RequestParam(required = false) String contactPerson,
                                                 @RequestParam(required = false) Integer title) {
        if (orgId == null) {
            orgId = userInfoHolder.getAdminOrgId();
        }
        Page<ApplicantPageVO> page = applicantService.getPage(pageNum, pageSize, orgId, applicantName, contactPerson, title);
        return Result.success(page);
    }

    /**
     * 导出集体
     */
    @GetMapping("/download")
    public void download(@RequestParam(required = false) Long orgId,
                         @RequestParam(value = "orgName", required = false) String applicantName,
                         @RequestParam(required = false) String contactPerson,
                         @RequestParam(required = false) Integer title) {
        if (orgId == null) {
            orgId = userInfoHolder.getAdminOrgId();
        }
        applicantService.download(orgId, applicantName, contactPerson, title);
    }

    /**
     * 重置密码
     */
    @GetMapping("/reset")
    public Result reset(@RequestParam Long applicantId) {
        applicantService.resetPassword(applicantId);
        return Result.success();
    }

    /**
     * 删除集体
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody List<Long> applicantIds) {
        applicantService.delete(applicantIds, userInfoHolder.getAdminOrgId());
        return Result.success();
    }

    /**
     * 查看集体联系信息
     */
    @GetMapping("/admin/info")
    public Result<ApplicantContactVO> getApplicant(@RequestParam Long applicantId) {
        ApplicantContactVO applicantVO = applicantService.getContact(applicantId);
        return Result.success(applicantVO);
    }

    /**
     * 修改集体联系信息
     */
    @PostMapping("/admin/update")
    public Result updateApplicant(@RequestBody @Valid ApplicantContactUpdateDTO applicantContactUpdateDTO) {
        applicantService.updateContact(applicantContactUpdateDTO);
        return Result.success();
    }

    /**
     * 查看台账
     */
    @GetMapping("/book/info")
    public Result<StandingBookVO> info(@RequestParam Long applicantId) {
        StandingBookVO standingBookVO = standingBookService.getByApplicantId(applicantId);
        return Result.success(standingBookVO);
    }

    /**
     * 查看台账资料
     */
    @GetMapping("/book/document")
    public Result<List<DocumentNumberVO>> getDocument(@RequestParam Long applicantId) {
        List<DocumentNumberVO> documentVOS = standingBookService.getDocumentByApplicantId(applicantId);
        return Result.success(documentVOS);
    }

    /**
     * 批量下载台账文档
     */
    @GetMapping("/book/document/download")
    public Result<String> download(@RequestParam Long applicantId) {
        String zipUrl = standingBookService.downloadDocumentByApplicantId(applicantId);
        return Result.success(zipUrl);
    }

    /**
     * 查看台账日志
     */
    @GetMapping("/log")
    public Result<List<StandingBookReviewLogVO>> getLog(@RequestParam Integer type) {
        List<StandingBookReviewLogVO> list = applicantService.getLogByAccountId(userInfoHolder.getAccountId(), type);
        return Result.success(list);
    }


}
